package com.gnss.common.session;

import com.gnss.common.proto.TerminalProto;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.tuple.MutablePair;

/**
 * @author guangbinhuang
 * @date 2018/11/1
 * @desc 通信会话
 **/
public class Session {

    /**
     * 终端信息
     */
    @Getter
    @Setter
    private TerminalProto terminalInfo;

    /**
     * 手机号数组
     */
    @Getter
    @Setter
    private byte[] phoneNumArr;

    /**
     * 消息流水号范围
     */
    private final MutablePair<Integer, Integer> msgFlowIdRange;

    /**
     * 当前消息流水号
     */
    private int currentMsgFlowId;

    public Session(Integer minMsgFlowId, Integer maxMsgFlowId, TerminalProto terminalInfo) {
        this.msgFlowIdRange = MutablePair.of(minMsgFlowId, maxMsgFlowId);
        this.terminalInfo = terminalInfo;
    }

    /**
     * 获取消息流水号，并设置下个消息流水号
     *
     * @return
     */
    public int getNextMsgFlowId() {
        int msgFlowId = currentMsgFlowId;
        int minMsgFlowId = msgFlowIdRange.getLeft();
        int maxMsgFlowId = msgFlowIdRange.getRight();
        currentMsgFlowId = currentMsgFlowId >= maxMsgFlowId ? minMsgFlowId : currentMsgFlowId + 1;
        return msgFlowId;
    }
}
